package com.xiaoyu.client.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.Poi;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.ui.activity.help.SeekAddActivity;
import com.xiaoyu.commonlib.ui.SpacesItemDecoration;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.ui.base.BaseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NearbyActivity extends BaseActivity {
    @BindView(R.id.page_refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.page_list_layout)
    RecyclerView mRecyclerView;

    public LocationClient mLocationClient = null;
    private AllPowerfulAdapter mLocateAdapter;
    private List<Poi> mLocateList = new ArrayList<>();
    private Poi mSelectPoi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby);
        ButterKnife.bind(this);
        parseIntent();
        initView();
    }

    private void parseIntent() {
        mSelectPoi = getIntent().getExtras().getParcelable(ClientConstants.Select_Location);
    }


    private void initView() {
        //刷新控件
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadMore(false);

        //地址列表
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        setLocationAdapter(mLocateList);

        mLocationClient = new LocationClient(getApplicationContext());
        mLocationClient.registerLocationListener(locationListener);
        LocationClientOption option = new LocationClientOption();
        //可选，是否需要周边POI信息，默认为不需要，即参数为false；需要参数为true
        option.setIsNeedLocationPoiList(true);
        option.setCoorType("GCJ02");
        option.setScanSpan(1000);
        option.setOpenGps(true);
        option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }

    private void setLocationAdapter(List<Poi> list){
        mLocateAdapter = new AllPowerfulAdapter<Poi>(R.layout.item_nearby_locate, list, new AllPowerfulAdapter.OnClickListener<Poi>() {
            @Override
            public void setOnClick(BaseViewHolder baseViewHolder, Poi poi, int position) {
                mSelectPoi = poi;
                mLocateAdapter.notifyDataSetChanged();
            }
        }){
            @Override
            protected void convert(BaseViewHolder baseViewHolder, Poi poi) {
                super.convert(baseViewHolder, poi);
                ImageView selectImg = baseViewHolder.getView(R.id.selected_locate_img);
                selectImg.setVisibility(View.INVISIBLE);
                if (mSelectPoi != null && mSelectPoi.getId().equals(poi.getId())){
                    selectImg.setVisibility(View.VISIBLE);
                }
                baseViewHolder.setText(R.id.location_name_txt,poi.getName());
            }
        };
        mRecyclerView.setAdapter(mLocateAdapter);

    }


    //返回按钮点击事件
    @OnClick(R.id.activity_nearby_title_back_btn)
    public void onBackClick(){
        Intent intent = new Intent();
        intent.putExtra(ClientConstants.Select_Location,mSelectPoi);
        setResult(RESULT_OK,intent);
        finish();
    }


    private BDAbstractLocationListener locationListener = new BDAbstractLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取周边POI信息相关的结果
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
            Log.i("xiaoyuu","经纬度  经度： "+bdLocation.getLongitude()+"   纬度：  "+bdLocation.getLatitude()
                    +"   街道： "+bdLocation.getStreet()+"   type = "+bdLocation.getLocType()
                    +"   省份 ： "+bdLocation.getProvince()+"  城市： "+bdLocation.getCity()+"  addstr : "+bdLocation.getAddrStr()
                    +"  描述："+bdLocation.getLocationDescribe());
            mLocateList.clear();
            mLocateList.addAll(bdLocation.getPoiList());
            mLocateAdapter.notifyDataSetChanged();
            mLocationClient.stop();
            //获取周边POI信息
            //POI信息包括POI ID、名称等，具体信息请参照类参考中POI类的相关说明
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationClient.stop();
    }
}
